package com.xmsx.back.controller.admin;

import com.xmsx.back.pojo.Graduate;
import com.xmsx.back.pojo.Organization;
import com.xmsx.back.results.Result;
import com.xmsx.back.service.admin.OrganizationService;
import com.xmsx.back.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class OrganizationController {
    @Autowired
    OrganizationService organizationService;

    /**
     * 添加单位信息
     *
     * @param orgName
     * @param orgRegion
     * @param grassrootType
     * @param defenseMilitary
     * @param collegeInstitute
     * @param keyNational
     * @param keyMedical
     * @param keyFinancial
     * @param keyMedia
     * @param office
     * @param keySoftware
     * @param informSafety
     * @return
     */
    @PutMapping("/organization")
    public Result<String> setOrganization(@RequestParam("orgName") String orgName,
                                          @RequestParam("orgRegion") String orgRegion,
                                          @RequestParam("grassrootType") int grassrootType,
                                          @RequestParam("defenseMilitary") int defenseMilitary,
                                          @RequestParam("collegeInstitute") int collegeInstitute,
                                          @RequestParam("keyNational") int keyNational,
                                          @RequestParam("keyMedical") int keyMedical,
                                          @RequestParam("keyFinancial") int keyFinancial,
                                          @RequestParam("keyMedia") int keyMedia,
                                          @RequestParam("office") int office,
                                          @RequestParam("keySoftware") int keySoftware,
                                          @RequestParam("informSafety") int informSafety) {

        Organization organization = new Organization();
        organization.setOrgName(orgName);
        organization.setOrgRegion(orgRegion);
        organization.setGrassrootType(grassrootType);
        organization.setDefenseMilitary(defenseMilitary);
        organization.setCollegeInstitute(collegeInstitute);
        organization.setKeyNational(keyNational);
        organization.setKeyMedical(keyMedical);
        organization.setKeyFinancial(keyFinancial);
        organization.setKeyMedia(keyMedia);
        organization.setOffice(office);
        organization.setKeySoftware(keySoftware);
        organization.setInformSafety(informSafety);

        organizationService.setOrganization(organization);
        return Result.success("添加成功！");
    }

    /**
     * 修改单位信息
     *
     * @param id
     * @param orgName
     * @param orgRegion
     * @param grassrootType
     * @param defenseMilitary
     * @param collegeInstitute
     * @param keyNational
     * @param keyMedical
     * @param keyFinancial
     * @param keyMedia
     * @param office
     * @param keySoftware
     * @param informSafety
     * @return
     */
    @PostMapping("/organization")
    public Result<String> updateOrganization(@RequestParam("id") int id,
                                             @RequestParam("orgName") String orgName,
                                             @RequestParam("orgRegion") String orgRegion,
                                             @RequestParam("grassrootType") int grassrootType,
                                             @RequestParam("defenseMilitary") int defenseMilitary,
                                             @RequestParam("collegeInstitute") int collegeInstitute,
                                             @RequestParam("keyNational") int keyNational,
                                             @RequestParam("keyMedical") int keyMedical,
                                             @RequestParam("keyFinancial") int keyFinancial,
                                             @RequestParam("keyMedia") int keyMedia,
                                             @RequestParam("office") int office,
                                             @RequestParam("keySoftware") int keySoftware,
                                             @RequestParam("informSafety") int informSafety) {

        Organization organization = new Organization();
        organization.setId(id);
        organization.setOrgName(orgName);
        organization.setOrgRegion(orgRegion);
        organization.setGrassrootType(grassrootType);
        organization.setDefenseMilitary(defenseMilitary);
        organization.setCollegeInstitute(collegeInstitute);
        organization.setKeyNational(keyNational);
        organization.setKeyMedical(keyMedical);
        organization.setKeyFinancial(keyFinancial);
        organization.setKeyMedia(keyMedia);
        organization.setOffice(office);
        organization.setKeySoftware(keySoftware);
        organization.setInformSafety(informSafety);

        organizationService.updateOrganization(organization);
        return Result.success("修改成功！");
    }

    @DeleteMapping("/organization")
    public Result<String> deleteOrganization(@RequestParam("id") int id) {
        Organization organization = organizationService.getById(id);
        if (null == organization) {
            // 信息不存在，无法删除
            return Result.error("单位信息不存在，删除失败");
        }
        organizationService.deleteOrganization(id);
        return Result.success("删除成功！");
    }

    @GetMapping("/organization")
    public Result<Organization> getOrganizationById(@RequestParam("id") int id) {
        Organization organization = organizationService.getById(id);
        if (organization != null) {
            return Result.success(organization);
        } else {
            return Result.error("不存在该条信息");
        }
    }

    /**
     * 获取单位信息列表
     *
     * @param pageCount
     * @param size
     * @return
     */
    @GetMapping("/organizationList")
    public Result<Map> getOrganizationList(@RequestParam("pageCount") int pageCount, @RequestParam("size") int size) {
        Pager pager = organizationService.findByPager(pageCount, size);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }

    /**
     * 按关键词模糊查询单位信息列表
     *
     * @param pageCount
     * @param size
     * @param orgName
     * @return
     */
    @GetMapping("/searchOrganization")
    public Result<Map> getOrganizationListByKeyword(@RequestParam("pageCount") int pageCount,
                                                    @RequestParam("size") int size,
                                                    @RequestParam("orgName") String orgName) {
        Pager pager = organizationService.findByKeywordByPager(pageCount, size, orgName);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }
}
